#include <iostream>
#include <stdlib.h>

#define totalSize 2000
#define maxSize 1000
typedef struct{
    int data;
    int weight;
    int lchild,rchild;
    int depth;
}HFNode;

typedef struct{
    HFNode elem[totalSize];
    int num;
    int root;
}HFTree;





void create(HFTree &Tree,HFTree &Finished){
    
    int l=0,r=1;
    if (Tree.elem[r].weight<Tree.elem[l].weight){r=0;l=1;}
    for (int i=0;i<Tree.num;i++){
        if (Tree.elem[i].weight<Tree.elem[r].weight){r=i;}
        if (Tree.elem[r].weight<Tree.elem[l].weight){r=l;l=i;}
    }//获得最小的两个编号
    HFNode NewNode;
    NewNode.weight=Tree.elem[l].weight+Tree.elem[l].weight;
    NewNode.lchild=l;
    NewNode.rchild=r;
    Finished.elem[Finished.num]=NewNode;
    Tree.num--;
    Finished.num++;

    if (Tree.num==1) return;
}

void insert(HFNode NewNode,HFTree &Finished){
    
    Finished.num++;
    int i=Finished.num-1;
    while(i!=0 && NewNode.weight>Finished.elem[i/2].weight){
		Finished.elem[i] =Finished.elem[i/2];
		i = i/2;
	}
    Finished.elem[i] = NewNode;
    Finished.elem[Finished.num]=Finished.elem[2*i+1];
    Finished.elem[2*i+1];
}
void cal(HFNode root,HFNode elem[]){
    
    
    if(root.lchild!=-1){
        elem[root.lchild].depth=root.depth+1;
        cal(elem[root.lchild],elem);}
    if(root.rchild!=-1){
        elem[root.rchild].depth=root.depth+1;
        cal(elem[root.rchild],elem);}
}

int *a;
int main(){
    int n;
    scanf("%d",&n);
    for (int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    a=(int *) malloc(n*sizeof(int));
    HFTree Tree;
    for (int i=0;i<n;i++){
        HFNode newnode;
        newnode.weight=a[i];
        Tree.root+=a[i];
        newnode.lchild=-1;
        newnode.rchild=-1;
    }
    HFTree Finished;
    Finished.num=0;
    Finished.root=0;
    create(Tree,Finished);
    
    HFNode root1=Finished.elem[Finished.root];
    root1.depth=0;
    cal(root1,Finished.elem);
    int sum=0;
    for (int i=0;i<Finished.num;i++){
        sum+=Finished.elem[i].depth*Finished.elem[i].weight;
    }
    printf("%d\n",sum);
}